package com.springsummer.information.green.webmvc;

import com.springsummer.information.green.annotation.ResponseBody;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

public class RequestMappingHandlerAdapterImpl implements HandlerAdapter {

    private DefaultHttpMessageConverter httpMessageConverter = new DefaultHttpMessageConverter();


    @Override
    public boolean supports(Object handler) {
        return true;
    }

    @Override
    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HandlerMethodInfo handlerMethod = (HandlerMethodInfo) handler;
        if (handlerMethod != null) {
            return invokeHandlerMethod(request, response, handlerMethod);
        }
        invokeDefaultServletHandle(request, response);
        return null;
    }

    protected ModelAndView invokeHandlerMethod(HttpServletRequest request, HttpServletResponse response, HandlerMethodInfo handlerMethod) throws Exception {


        Method method = handlerMethod.getMethod();
        Object obj = handlerMethod.getHandleBean();
        Object returnObj = null;
        try {
            // 参数设置
            RequestParameterMapper requestParameterMapper = new RequestParameterMapper();
            Object[] argsArray = requestParameterMapper.requestParametersMethodArgsMapping(method, request);

            returnObj = method.invoke(obj, argsArray);
        } catch (Exception e) {
            e.printStackTrace();
        }

        ModelAndView mav = null;
        //如果是ResponseBody注解，仅仅返回值，则转换数据格式后直接写到response
        if (handlerMethod.getMethod().isAnnotationPresent(ResponseBody.class)) { //ResponseBody
            this.httpMessageConverter.write(returnObj, response);
        } else { //返回的是前端页面
            if (returnObj instanceof ModelAndView) {
                mav = (ModelAndView) returnObj;
            } else if (returnObj instanceof String) { //字符串也认为是前端页面
                String viewname = (String) returnObj;
                mav = new ModelAndView();
                mav.setViewName(viewname);
            }
        }

        return mav;
    }

    private void invokeDefaultServletHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ServletContext servletContext = request.getServletContext();
        RequestDispatcher rd = servletContext.getNamedDispatcher("default");
        if (rd == null) {
            throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" +
                    "");
        }
        rd.forward(request, response);
    }

    private ModelAndView handleInternalTwo(HttpServletRequest request, HttpServletResponse response,
                                           HandlerMethodInfo handler) {
        Method method = handler.getMethod();
        Object obj = handler.getHandleBean();
        Object objResult = null;
        try {
            objResult = method.invoke(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ModelAndView("");
    }

}
